package com.ordering.dao.order.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.ordering.constant.ResultResponse;
import com.ordering.dao.order.OrderItemDao;
import com.ordering.dao.product.ProductDao;
import com.ordering.dao.product.impl.ProductDaoImpl;
import com.ordering.entity.OrderItem;
import com.ordering.entity.Product;
import com.ordering.util.HibernateUtil;

public class OrderItemDaoImpl implements OrderItemDao{
	private SessionFactory sessionFactory;
    public SessionFactory getSessionFactory() {
        return sessionFactory;
    }
    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }
    
    ProductDao productDao = new ProductDaoImpl();
    
	@Override
	public int add(OrderItem orderItem) throws Exception {
		Session s=null;
        ResultResponse resultResponse = new ResultResponse();
        try{
            s=HibernateUtil.getSession();
            
            Transaction tran = s.beginTransaction();
            //获取商品对象
            Product product = productDao.getById(orderItem.getProductId());
            orderItem.setPrice(product.getRealPrice());
            orderItem.setProductName(product.getName());
            orderItem.setProductNum(1);
            orderItem.setProductType(product.getType());
            s.save(orderItem);
            tran.commit();
            return 1;
        }catch (Exception e) {
            resultResponse.setMessage(e.getMessage());
            e.printStackTrace();
        }finally{
            if(s!=null)
                s.close();
        }
        return 0;
	}

	@Override
	public int delect(OrderItem orderItem) throws Exception {
		Session s=null;
        try
        {
            s=HibernateUtil.getSession();
            Transaction tran = s.beginTransaction();
            Query q = s.createQuery("delete from OrderItem where id = ?");
            q.setInteger(0, orderItem.getId());
            q.executeUpdate();
            tran.commit();
            return 1;
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            if(s!=null)
                s.close();
        }
        return 0;
	}

	@Override
	public List<OrderItem> getAll(OrderItem orderItem) throws Exception {
		Session s=null;
        s = HibernateUtil.getSession();
        
        //根据orderItem带来的参数拼接查询条件
        StringBuffer sb = new StringBuffer("from OrderItem where 1=1 ");
        //状态
        if(orderItem.getStatus() > 0){
            sb.append(" and status=? ");
        }
        //用户id
        if(orderItem.getUserId() > 0){
            sb.append(" and userId=? ");
        }
        //订单id
        if(orderItem.getOrderId() > 0){
            sb.append(" and orderId=? ");
        }
        //sb.append(" order by create_time desc ");
        
        Query q = s.createQuery(sb.toString());
        
        int i = 0;
        //状态
        if(orderItem.getStatus() > 0){
            q.setInteger(i++, orderItem.getStatus());
        }
        //用户id
        if(orderItem.getUserId() > 0){
            q.setInteger(i++, orderItem.getUserId());
        }
        //订单id
        if(orderItem.getOrderId() > 0){
            q.setInteger(i++, orderItem.getOrderId());
        }

        List<OrderItem> list = q.list(); 
        
        if(s != null)
            s.close();
        
        return list;
	}

	@Override
	public boolean updateStatus(List<OrderItem>  orderItemList) throws Exception {
		Session s=null;
        s=HibernateUtil.getSession();
        Transaction tran = s.beginTransaction();
        StringBuffer sb = new StringBuffer("update OrderItem set status = ? ,orderId=? ");

        //根据id更行
        sb.append(" where id in (:idList) ");
        
        Query q = s.createQuery(sb.toString());
        
        //插入状态
        q.setInteger(0, orderItemList.get(0).getStatus());
        //插入订单id
        q.setInteger(1, orderItemList.get(0).getOrderId());
        
        List<Integer> idList = new ArrayList<Integer>();
        for(OrderItem item : orderItemList){
        	idList.add(item.getId());
        }
        
        //插入id
        q.setParameterList("idList", idList);
        	
        boolean result = q.executeUpdate() > 0;
        
        tran.commit();
        if(s != null)
        s.close();
        return result;
	}
	@Override
	public List<OrderItem> getAllByIds(List<Integer>  idList) throws Exception {
		Session s=null;
        s=HibernateUtil.getSession();
        //s.clear();
        Transaction tran = s.beginTransaction();
        
        StringBuffer sb = new StringBuffer("from OrderItem as t where t.id in (:idList)");

        Query q = s.createQuery(sb.toString());
        q.setParameterList("idList", idList);
        
        q.setMaxResults(Integer.MAX_VALUE);
        List<OrderItem> list = (List<OrderItem>)q.list(); 
        
        tran.commit();
        if(s != null)
        s.close();
        
        return list;

	}
}
